package com.zds.scm.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.zds.scm.common.util.StringUtils.*;
import com.zds.scm.common.CommonServlet;
import com.zds.scm.domain.DeptInfo;
import com.zds.scm.service.DeptInfoService;
import com.zds.scm.service.impl.DeptInfoServiceImpl;

public class DeptInfoServlet extends CommonServlet {

	private static final long serialVersionUID = 4475040933223556033L;
	private DeptInfoService deptInfoService=new DeptInfoServiceImpl();
	private DeptInfo deptInfo=new DeptInfo();
	
	public void setDeptInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    logger.debug("获取参数setDeptInfo!");
		deptInfo.setDeptCode(null2Empty(request.getParameter("deptCode")));
	    deptInfo.setDeptName(null2Empty(request.getParameter("deptName")));
	    deptInfo.setDeptDesc(null2Empty(request.getParameter("deptDesc")));
	    logger.debug("deptCode="+deptInfo.getDeptCode());
	    logger.debug("deptName="+deptInfo.getDeptName());
	}
	public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行insert方法");
		try {
			setDeptInfo(request, response);
			DeptInfo d1=deptInfoService.findById(deptInfo.getDeptCode());
			if(d1==null){
			  logger.debug("不存在新增");
			  if(deptInfoService.insert(deptInfo)){
				request.setAttribute("message", "新增系别成功");
				request.setAttribute("statusCode", 200);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			  }else {
				request.setAttribute("message", "新增系别失败,不存在系别:deptCode="+deptInfo.getDeptCode());
				request.setAttribute("statusCode", 300);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			  }
			}else {
				logger.debug("存在修改");
				if(deptInfoService.update(deptInfo)){
					request.setAttribute("message", "修改系别成功");
					request.setAttribute("statusCode", 200);
					request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
				}else {
					request.setAttribute("message", "修改系别失败,不存在系别:deptCode="+deptInfo.getDeptCode());
					request.setAttribute("statusCode", 300);
					request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
				}
			}
		} catch (Exception e) {
			request.setAttribute("message",e.getMessage());
			request.setAttribute("statusCode", 300);
			request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
		}
	}
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行delete方法");
		try {
			setDeptInfo(request, response);
			if(deptInfoService.delete(deptInfo.getDeptCode())){
				request.setAttribute("message", "删除系别成功");
				request.setAttribute("statusCode", 200);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			}else {
				request.setAttribute("message", "删除系别失败,不存在系别:deptCode="+deptInfo.getDeptCode());
				request.setAttribute("statusCode", 300);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			}
		} catch (Exception e) {
			request.setAttribute("message",e.getMessage());
			request.setAttribute("statusCode", 300);
			request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
		}
	}
	public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行update方法");
		try {
			setDeptInfo(request, response);
			if(deptInfoService.update(deptInfo)){
				request.setAttribute("message", "修改系别成功");
				request.setAttribute("statusCode", 200);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			}else {
				request.setAttribute("message", "修改系别失败,不存在系别:deptCode="+deptInfo.getDeptCode());
				request.setAttribute("statusCode", 300);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			}
		} catch (Exception e) {
			request.setAttribute("message",e.getMessage());
			request.setAttribute("statusCode", 300);
			request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
		}
	}
	public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行findById方法");
		try {
			setDeptInfo(request, response);
			request.setAttribute("deptInfo", deptInfoService.findById(deptInfo.getDeptCode()));
			request.getRequestDispatcher("/pages/common/deptinfoedit.jsp").forward(request, response);
		} catch (Exception e) {
			request.setAttribute("message",e.getMessage());
			request.setAttribute("statusCode", 300);
			request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
		}
	}
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行findAll方法");
		setDeptInfo(request, response);
		List<DeptInfo> ld=deptInfoService.findAll(getCurrentPage(),getPageSize());
		request.setAttribute("deptInfos", ld);
		request.setAttribute("totalCount", deptInfoService.getTotalRows());
		request.getRequestDispatcher("/pages/common/deptinfo.jsp").forward(request, response);
	}
	public void findByNameAndCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行findByNameAndCode方法");
		setDeptInfo(request, response);
		List<DeptInfo> ld=deptInfoService.findByNameAndCode(deptInfo.getDeptName(), deptInfo.getDeptCode(), getCurrentPage(), getPageSize());
		request.setAttribute("deptInfos", ld);
		request.setAttribute("totalCount", deptInfoService.getTotalRowsByNameAndCode(deptInfo.getDeptName(), deptInfo.getDeptCode()));
		request.getRequestDispatcher("/pages/common/deptinfo.jsp").forward(request, response);
	}
}
